<?php

namespace App\Models;

use App\Common\Constant\Params;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

class MasterTradeLog extends BaseModel
{
    protected $table = 'master_trade_log';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'trade_number',
        'master_id',
        'master_name',
        'direction',
        'amount',
        'type',
        'source',
        'balance',
        'assure_fund',
        'order_number',
        'order_serial_number',
        'ip',
        'status',
        'error',
        'info',
        'transaction_id',
    ];

    protected $casts = [
        'amount' => 'double',
        'balance' => 'double',
        'assure_fund' => 'double',
        'service_type' => 'array',
    ];

    public function assoc(){
        Relation::morphMap([
            Params::TRADE_TYPE_ASSURE => MasterFund::class,
            Params::TRADE_TYPE_ORDER => Order::class,
            Params::TRADE_TYPE_CASH => MasterCash::class,
            Params::TRADE_TYPE_REFUND => OrderRefund::class,
            Params::TRADE_TYPE_PUNISH => OrderComplain::class,
            Params::TRADE_TYPE_RETURN_ASSURE => MasterFund::class,
        ]);
        return $this->morphTo(null,'type','order_number');
    }

    /**
     * 交易类型文字
     * type_text
     */
    public function getTypeTextAttribute()
    {
        return Params::$TRADE_TYPE[$this->type];
    }

    /**
     * 支付来源文字
     * source_text
     */
    public function getSourceTextAttribute()
    {
        return Params::$TRADE_SOURCE[$this->source];
    }

    /**
     * 交易方向文本
     * direction_text
     */
    public function getDirectionTextAttribute()
    {
        return [
            Params::TRADE_DIRECTION_IN=>'+',
            Params::TRADE_DIRECTION_OUT=>'-'
        ][$this->direction];
    }
}
